#include "navigator.h"

Navigator::Navigator(QObject *parent) :
    QObject(parent), m_currentPosition(-1)
{
}

int Navigator::currentPosition() const
{
    return m_currentPosition;
}

QVariant Navigator::currentElement() const
{
    return m_elements.at(currentPosition());
}

int Navigator::elementCount() const
{
    return m_elements.size();
}

void Navigator::addElement (const QString& name, const QVariant& data)
{
    // if current position is not the last, remove elements after the current position
    if (currentPosition() < m_elements.size()-1)
    {
        while (currentPosition() < m_elements.size()-1)
            removeLastElement();
    }

    m_elements.append(data);
    setCurrentPosition(m_elements.size()-1);

    emit elementAdded(name);
}

void Navigator::addElementToPosition (const int position, const QString& name, const QVariant& data)
{
    Q_ASSERT(position <= m_elements.size());
    if (position > m_elements.size())
        return;

    while (position < m_elements.size())
        removeLastElement();

    addElement(name, data);
    return;
}

void Navigator::removeElementsFrom (const int position)
{
    Q_ASSERT(position > -1);
    while (m_elements.size() > position)
        removeLastElement();
}

void Navigator::removeLastElement()
{
    emit dataAboutToBeRemoved(m_elements.last());
    m_elements.removeLast();
    emit lastElementRemoved();
}

void Navigator::setCurrentPosition (const int position)
{
    m_currentPosition = position;
}
